/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.reportserver.util;

import de.tor.tribes.types.ZebraReport;
import de.tor.tribes.types.ZebraReportUnit;

/**
 *
 * @author Torridity
 */
public class UnitTableBuilder {

    private static final String[] units = new String[]{"spear", "sword", "axe", "archer", "spy", "light", "marcher", "heavy", "ram", "catapult", "knight", "snob", "militia"};

    public static String createUnitTableLink(ZebraReport pReport, ZebraReportUnit.TYPE pType) {

        switch (pType) {
            case ATTACKER:
                return createAttackerTableLink(pReport, new ZebraReportUnit.TYPE[]{ZebraReportUnit.TYPE.ATTACKER, ZebraReportUnit.TYPE.LOST_ATTACKERS});
            case DEFENDER:
                return createAttackerTableLink(pReport, new ZebraReportUnit.TYPE[]{ZebraReportUnit.TYPE.DEFENDER, ZebraReportUnit.TYPE.LOST_DEFENDERS});
            default: //other
        }

        return null;
    }

    private static String createAttackerTableLink(ZebraReport pReport, ZebraReportUnit.TYPE[] pRows) {
        StringBuilder b = new StringBuilder();
        b.append("http://www.dsworkbench.de/tools/unitTable.php?in=");

        for (int i = 0; i < units.length; i++) {
            ZebraReportUnit unit = pReport.findUnitByPlainName(units[i], pRows[0]);
            b.append(i).append(".").append((unit == null) ? 0 : unit.getAmount());
            if (i < units.length - 1) {
                b.append("_");
            }
        }

        b.append("&out=");

        for (int i = 0; i < units.length; i++) {
            ZebraReportUnit unit = pReport.findUnitByPlainName(units[i], pRows[1]);
            b.append(i).append(".").append((unit == null) ? 0 : unit.getAmount());
            b.append("_");
        }
        String result = b.toString();
        return result.substring(0, result.length() - 1);
    }

    private static String createAttackerTableLink(ZebraReport pReport, ZebraReportUnit.TYPE pRows) {
        //for single row
        return null;
    }
}